(function() {

    angular.module('BlurAdmin.pages.console')
        .controller('linear1Ctrl', linear1Ctrl);

    function linear1Ctrl($http, $myconfig, $scope, $datepicker, $filter) {

        $scope.opt = {
            "date": "day" //月：month，年：year，日：day
        };
        var opt = {
            "date": ""
        }




        //追加年:'y '   //追加月"M " :   //追加天"d "
        function DateAdd(interval, number, date) {
            switch (interval) {
                case "y ": //追加年
                    {
                        date.setFullYear(date.getFullYear() + number);
                        return date;
                        break;
                    }
                case "q ":
                    {
                        date.setMonth(date.getMonth() + number * 3);
                        return date;
                        break;
                    }
                case "M ": //追加月
                    {
                        date.setMonth(date.getMonth() + number);
                        return date;
                        break;
                    }
                case "w ":
                    {
                        date.setDate(date.getDate() + number * 7);
                        return date;
                        break;
                    }
                case "d ": //追加天
                    {
                        date.setDate(date.getDate() + number);
                        return date;
                        break;
                    }
                case "h ":
                    {
                        date.setHours(date.getHours() + number);
                        return date;
                        break;
                    }
                case "m ":
                    {
                        date.setMinutes(date.getMinutes() + number);
                        return date;
                        break;
                    }
                case "s ":
                    {
                        date.setSeconds(date.getSeconds() + number);
                        return date;
                        break;
                    }
                default:
                    {
                        date.setDate(d.getDate() + number);
                        return date;
                        break;
                    }
            }
        }

        var x_data = [1, 2, 3];
        var is_url = $myconfig.api.Physicallogs_line_chart; //用户使用情况统计折线图   $myconfig.api.Physicallogs_line_chart_dev; //控制台折线新增设备数据统计
        $scope.tpo2 = 1;

        $scope.btn = function(val) {
            $scope.tpo2 = val;
            if (val == 1) {
                is_url = $myconfig.api.Physicallogs_line_chart;
                $scope.shuju($scope.opt.date);
            }
            if (val == 2) {
                is_url = $myconfig.api.Physicallogs_line_chart_dev;
                $scope.shuju($scope.opt.date);
            }
        }

        $scope.shuju = function(val) {
            $scope.opt.date = val;
            opt.date = $scope.opt.date;
            var data_type = 'yyyy-MM-dd';
            var ole_type = 'd ';

            if (opt.date == 'day') {
                data_type = 'yyyy-M-d';
                ole_type = 'd ';
            }
            if (opt.date == 'month') {
                data_type = 'yyyy-M';
                ole_type = 'M ';

            }
            if (opt.date == 'year') {
                data_type = 'yyyy';
                ole_type = 'y ';
            }

            $http.get(is_url, { params: opt }).then(function(res) {
                $scope.bigTotalItems = res.data.data;
                var lin_jbg_data = res.data.data[0].value;
                var lin_hyb_data = res.data.data[1].value;

                /* 计算时间把时间区间算出来 */
                var aa = res.data.data[2].least_time; //最小时间
                var cc = $filter('date')(aa, 'yyyy-MM-dd hh:mm:ss');
                var old_d = new Date(Date.parse(cc.replace(/-/g, "/"))); //转化为Date对象的最小时间
                var new_date = (new Date());
                var new_date_getTime = new_date.getTime() //当天的时间戳
                var tingzhi = true;
                var Date_arr = [];
                for (var i = 1; tingzhi; i++) {
                    var old_d_getTime = old_d.getTime(); //需要push的时间戳
                    if (old_d_getTime > (new_date_getTime - 86400000)) { //判断时间戳是否小于
                        tingzhi = false;
                    } else {
                        if (i == 1) { //第一次先把最小天数添加上
                            var date_22 = $filter('date')(old_d, data_type);
                            Date_arr.push(date_22);
                        } else {
                            old_d = DateAdd(ole_type, 1, old_d);
                            var date_ = $filter('date')(old_d, data_type);
                            Date_arr.push(date_);
                        }
                    }
                }

                Date_arr.pop();

                //赋值给折线数据
                var jbg_arr = [];
                var hyb_arr = [];

                var j = 0;
                var x = 0;
                for (var n = 0; n < Date_arr.length; n++) {
                    jbg_arr[n] = { x: Date_arr[n], y: 0 }; //赋值日期，检测此日期是否有值
                    hyb_arr[n] = { x: Date_arr[n], y: 0 };

                    if (lin_jbg_data[j] && jbg_arr[n].x == lin_jbg_data[j].time) {
                        jbg_arr[n] = { x: Date_arr[n], y: lin_jbg_data[j].num };
                        jbg_arr[n].y = lin_jbg_data[j].num;
                        j++;
                    }
                    if (lin_hyb_data[x] && hyb_arr[n].x == lin_hyb_data[x].time) {
                        hyb_arr[n] = { x: Date_arr[n], y: lin_hyb_data[x].num };
                        hyb_arr[n].y = lin_hyb_data[x].num;
                        x++;
                    }

                    jbg_arr[n] = jbg_arr[n].y;
                    hyb_arr[n] = hyb_arr[n].y;

                }
                x_data = Date_arr;
                // jbg_arr
                var myChart = echarts.init(document.getElementById('main'));
                // hyb_arr
                option.xAxis.data = x_data;


                option.series[0].data = jbg_arr;
                option.series[1].data = hyb_arr;
                myChart.setOption(option);

            })
        }
        $scope.shuju('day');



        var option = {

            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data: ['金鼻光', '换颜宝']
            },
            toolbox: {
                show: true,
                feature: {
                    dataZoom: {
                        yAxisIndex: 'none'
                    },
                    dataView: { readOnly: false },
                    magicType: { type: ['line', 'bar'] },
                    restore: {},
                    saveAsImage: {}
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: [1, 2, 3]
            },
            yAxis: {
                type: 'value',
                axisLabel: {
                    formatter: '{value}'
                }
            },
            dataZoom: {
                type: 'line',
                show: true,
                start: 1,
                end: 100
            },
            series: [{
                    name: '金鼻光',
                    type: 'line',
                    data: [11, 11, 15, 13, 12, 13, 10], //
                    markPoint: {
                        data: [
                            { type: 'max', name: '最大值' },
                            { type: 'min', name: '最小值' }
                        ]
                    },
                    markLine: {
                        data: [
                            { type: 'average', name: '平均值' }
                        ]
                    }
                },
                {
                    name: '换颜宝',
                    type: 'line',
                    data: [1, -2, 2, 5, 3, 2, 0],
                    markPoint: {
                        data: [
                            { type: 'max', name: '最大值' },
                            { type: 'min', name: '最小值' }
                        ]
                    },
                    markLine: {
                        data: [
                            { type: 'average', name: '平均值' },
                            [{
                                symbol: 'none',
                                x: '90%',
                                yAxis: 'max'
                            }, {
                                symbol: 'circle',
                                label: {
                                    normal: {
                                        position: 'start',
                                        formatter: '最大值'
                                    }
                                },
                                type: 'max',
                                name: '最高点'
                            }]
                        ]
                    }
                }
            ]
        };


    }

})();